import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import Home from '../components/Home.vue'
import User from '../components/user/user.vue'
import Welcome from '../components/Welcome.vue'
import Patient from '../components/patient/patient.vue'
import Doctors from '../components/doctors/doctors.vue'
import Hospital from '../components/hospital/hospital.vue'
import Hello from '../components/hello/hello.vue'
import Registration from '../components/registration/registration.vue'
import ItemCat from '../components/items/ItemCat.vue'
import Item from '../components/items/Item.vue'
import AddItem from '../components/items/addItem.vue'
import Intro from '../components/intro/intro.vue'
import Lead from '../components/lead/lead.vue'
import Doctor from '../components/doctors/doctor/doctors.vue'
import AddDoctor from '../components/doctors/doctor/adddoctor.vue'
import Map from '../components/map/map.vue'
import Honor from '../components/honor/honor.vue'
import AddHonor from '../components/honor/addhonor.vue'
import Advisory from '../components/advisory/advisory.vue'
import AddAdvisory from '../components/advisory/addadvisory.vue'

Vue.use(VueRouter)
const routes = [
  {path: '/', redirect: '/login'},
  {path: '/login', component: Login},
  {path: '/home', component: Home, redirect: '/welcome', children: [
    {path: '/user', component: User},
    {path: '/welcome', component: Welcome},
    {path: '/patient',component: Patient},
    {path: '/hospital',component:Hospital},
    {path: '/doctors',component:Doctors},
    {path: '/hello',component:Hello},
    {path: '/registration',component:Registration},
    {path: '/item', component: Item},
        {path: '/itemCat', component: ItemCat},
        {path: '/item/addItem', component: AddItem},
		{path: '/intro', component: Intro},
    {path: '/lead', component: Lead},
    {path: '/doctor',component:Doctor},
    {path: '/doctor/adddoctor',component:AddDoctor},
    {path: '/map',component:Map},
    {path: '/honor',component:Honor},
    {path: '/addhonor',component:AddHonor},
    {path: '/advisory',component:Advisory},
    {path: '/addadvisory',component:AddAdvisory}
  ]},

]

const router = new VueRouter({
  routes
})

//定义路由导航守卫 考点: 拦截器
/*
    1.遍历的每个路由都会执行回调函数.
    2.参数信息: 3个
        2.1 to: 请求访问的地址 到哪去
        2.2 from: 请求从哪里跳转而来 从哪来
        2.3 next: 是一个函数  next() 请求放行 next("/login")发起login请求

 */
/* router.beforeEach((to,from,next) => {
    //1.如果用户访问/login的请求,应该直接放行
    if(to.path === '/login') return next()

    //不是访问的登录页面,所以判断用户是否登录 判断依据token
    let token = window.sessionStorage.getItem("token")
    //如果token有值,则执行if之后的操作
    if(!token) return next('/login')

    //如果代码执行到这一行,说明用户已经登录,则放行
    next()

}) */
/* 同上 */
router.beforeEach((to,from,next) => {
  if(to.path === "/login") return next()
  let token = window.sessionStorage.getItem("token");
  if(!token) return next("/login")
  next()
})



export default router
